# Archlive <http://archlive-pkg.googlecode.com>

_kernver=`uname -r`
_xorg_ver=11
_max_org_ver=$((${_xorg_ver}+1))
pkgbase="virtualbox-guest-modules-cjk"
pkgname=(virtualbox-guest-modules-cjk virtualbox-guest-additions)
basever=4.0.6
_build=71344
pkgver=${basever}_${_build}
#pkgver=$basever
pkgrel=1
arch=('i686' 'x86_64')
url='http://virtualbox.org'
license=('GPL2')
depends=("kernel26=${_kernver}")
makedepends=('gcc' 'make' 'kernel26-headers' 'p7zip')
optdepends=()
provides=()
backup=()

_arch='x86'
[ "${CARCH}" = 'x86_64' ] && _arch='amd64'

source=("http://download.virtualbox.org/virtualbox/${basever}/VBoxGuestAdditions_${basever}.iso")
noextract=(VBoxGuestAdditions_${basever}.iso)
md5sums=('d3c3d1848cfcb005f86db622d8a3f7db')

build() {

	cd $srcdir
	[ -d src ] && rm -rf src
	mkdir src
	cd src
	7z x -y ../VBoxGuestAdditions_${basever}.iso

	cd $srcdir
	[ -d VBoxLinuxAdditions ] && rm -rf VBoxLinuxAdditions
	# Unpack the run package via sh(1)
	sh src/VBoxLinuxAdditions.run --target "VBoxLinuxAdditions" --nox11 --noexec &> /dev/null

	cd VBoxLinuxAdditions
	tar -xvpf VBoxGuestAdditions-${_arch}.tar.bz2

	[ -d $srcdir/vboxguest ] && rm -rf $srcdir/vboxguest
	cp -a src/vboxguest-$basever $srcdir/vboxguest
	cd $srcdir/vboxguest
	make
}
package_virtualbox-guest-modules-cjk(){
	pkgdesc="VirtualBox kernel modules for Linux guests"
	replaces=('virtualbox-ose-additions-modules')
	provide=(virtualbox-guest-modules "virtualbox-guest-modules=$pkgver" "virtualbox-guest-modules=${_kernver}")
	depends=('kernel26>=2.6.39' 'kernel26<2.6.40')
	install='virtualbox-guest.install'

	# install kernel modules
	for i in vboxguest vboxsf vboxvideo; do
		install -Dm755 $srcdir/vboxguest/$i.ko $pkgdir/lib/modules/${_kernver}/misc/$i.ko
	done
	#gzip -9 $pkgdir/lib/modules/${_kernver}/misc/*.ko
}

package_virtualbox-guest-additions(){
    pkgdesc="VirtualBox user-space tools for Linux guests"
    depends=("virtualbox-guest-modules=$pkgver" 'gcc-libs' 'libxmu' 'xorg-xrandr')
    replaces=('virtualbox-ose-additions')
    conflicts=('virtualbox-ose-additions' "xorg-server>1.${_max_org_ver}.0")

    cd "$srcdir/VBoxLinuxAdditions"

    install -d "$pkgdir/usr"
    for dir in bin lib share src; do
	cp -a $dir $pkgdir/usr/
    done

    for bin in VBoxClient VBoxClient-all VBoxControl; do
	chmod 755 $pkgdir/usr/bin/$bin
    done
    install -Dm755 lib/VBoxGuestAdditions/mount.vboxsf $pkgdir/sbin/mount.vboxsf
    install -Dm755 sbin/VBoxService $pkgdir/usr/bin/VBoxService

    install -Dm644 share/VBoxGuestAdditions/50-vboxmouse.conf \
        $pkgdir/etc/X11/xorg.conf.d/50-vboxmouse.conf
    install -Dm644 share/VBoxGuestAdditions/70-xorg-vboxmouse.rules \
        $pkgdir/lib/udev/rules.d/70-xorg-vboxmouse.rules
    install -Dm755 share/VBoxGuestAdditions/vboxclient.desktop \
        $pkgdir/etc/xdg/autostart/vboxclient.desktop

    install -d "$pkgdir/usr/lib/xorg/modules/dri"
    install -m755 lib/VBoxOGL*.so "$pkgdir/usr/lib"
    ln -s /usr/lib/VBoxOGL.so "$pkgdir/usr/lib/xorg/modules/dri/vboxvideo_dri.so"
    install -Dm755 lib/VBoxGuestAdditions/pam_vbox.so "$pkgdir/lib/security/pam_vbox.so"

    LIB="$pkgdir/usr/lib"
    mkdir -p $LIB
    cp -a $srcdir/VBoxLinuxAdditions/lib/VBoxGuestAdditions $LIB/
    rm -f $LIB/VBoxGuestAdditions/mount.vboxsf

    mkdir -p $pkgdir/usr/lib/xorg/modules/{input,drivers}
    ln -s /usr/lib/VBoxGuestAdditions/vboxmouse_drv_1${_xorg_ver}.so \
	"$pkgdir/usr/lib/xorg/modules/input/vboxmouse_drv.so"
    ln -s /usr/lib/VBoxGuestAdditions/vboxvideo_drv_1${_xorg_ver}.so \
	"$pkgdir/usr/lib/xorg/modules/drivers/vboxvideo_drv.so"

   mkdir -p $pkgdir/etc/rc.d
   for i in vboxadd vboxadd-service vboxadd-x11; do
	ln -s /usr/lib/VBoxGuestAdditions/$i $pkgdir/etc/rc.d/$i
   done
}
